// 作者关注组件
<template>
  <van-button
    v-if="value"
    class="follow-btn"
    round
    size="small"
    :loading="loading"
    @click="onFollow"
    >已关注</van-button
  >

  <van-button
    v-else
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    :loading="loading"
    @click="onFollow"
    >关注</van-button
  >
</template>

<script>
import { addFollow, deleteFollow } from "@/api/user";
export default {
  naem: "FollowUser",
  props: {
    value: {
      type: Boolean,
      required: true,
    },
    userId: {
      type: [Number, String, Object],
      required: true,
    },
  },
  data() {
    return {
      loading: false,
    };
  },
  methods: {
    async onFollow() {
      this.loading = true; //展示关注按钮的loading状态
      try {
        if (this.value) {
          // 已关注 取消关注
          await deleteFollow(this.userId);
        } else {
          // 未关注，关注
          await addFollow(this.userId);
        }
        // 更新视图
        // this.$emit("update-is_followed",!this.value);
        this.$emit("input",!this.value)
      } catch (err) {
        console.log(err);
        let message = "操作失败";
        if (err.response && err.response.status === 400) {
          message = "您不能关注自己";
        }
        this.$toast(message);
      }
       this.loading = false //关闭loading
    },
  },
};
</script>

<style>
</style>